
package com.hlkj.pay.app.merchant.ext.impl.helipay;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.framework.extension.executor.ExtensionExecutor;
import com.hlkj.framework.extension.scenario.BizScenario;
import com.hlkj.pay.app.merchant.ext.ChannelMerchantOrderExtPt;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelPayOrderContext;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelPayOrderQueryContext;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelRefundPayOrderContext;
import com.hlkj.pay.app.merchant.ext.impl.helipay.constant.HeliPayConstant;
import com.hlkj.pay.app.merchant.ext.impl.helipay.constant.HeliPayExtConstant;
import com.hlkj.pay.app.merchant.ext.impl.helipay.dto.HeliPayAppChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.helipay.dto.HeliPayConfig;
import com.hlkj.pay.app.merchant.ext.impl.helipay.dto.res.HeliPayOrderAccountRes;
import com.hlkj.pay.app.merchant.ext.impl.helipay.dto.res.HeliPayOrderNotifyRes;
import com.hlkj.pay.app.merchant.ext.impl.helipay.dto.res.HeliPayRefundNotifyRes;
import com.hlkj.pay.app.merchant.ext.impl.helipay.ext.HeliPaySceneCommonPayOrder;
import com.hlkj.pay.app.merchant.ext.impl.helipay.handler.HeliPayMerchantCommonParamsHandle;
import com.hlkj.pay.app.merchant.ext.impl.helipay.util.HeliPaySM2Utils;
import com.hlkj.pay.common.MerchantResultCode;
import com.hlkj.pay.dto.order.PayOrderChannelResultDto;
import com.hlkj.pay.dto.order.PayOrderFundDto;
import com.hlkj.pay.dto.order.notify.CsOrderNotifyDto;
import com.hlkj.pay.dto.order.notify.RefundOrderNotifyDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.PayEnums;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.infrastructure.model.order.OrderFundDetailDO;
import com.hlkj.pay.infrastructure.model.order.PayOrderDO;
import com.hlkj.pay.infrastructure.model.order.RefundOrderDO;
import com.hlkj.pay.infrastructure.model.pay.PayWayDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.hlkj.pay.util.DateUtils;
import com.hlkj.pay.util.HttpRequestUtil;
import com.hlkj.pay.util.MerchantUtils;
import com.hlkj.pay.vo.order.req.PayOrderCommonParamReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 汇付订单操作
 * 
 * @author HlpayTeam
 * @date 2024/10/10 13:51
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = MerchantExtConstant.CHANNEL_HELIPAY)
public class HeliPayMerchantOrderExtPt implements ChannelMerchantOrderExtPt {

    private final ExtensionExecutor extensionExecutor;

    private final HeliPayMerchantCommonParamsHandle heliPayMerchantCommonParamsHandle;

    private final HeliPaySceneCommonPayOrder heliPaySceneCommonPayOrder;

    public static Set<String> payOrderSignFields;

    public static Set<String> refundOrderSignFields;

    @Override
    public CommonResult<String> channelOrderCode(ChannelPayOrderContext channelPayOrderContext) {
        return CommonResult.success();
    }

    @Override
    public void submitChannelPayOrder(ChannelPayOrderContext channelPayOrderContext) {
        PayWayDO payWayDO = channelPayOrderContext.getPayWayDO();
        PayOrderDO payOrderDO = channelPayOrderContext.getPayOrderDO();
        String huiFuPay = heliPayType(payWayDO, channelPayOrderContext.getExtra());
        if (!StringUtils.hasText(huiFuPay)) {
            channelPayOrderContext.setRemark("当前支付方式不支持");
            channelPayOrderContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        if (StringUtils.hasText(payOrderDO.getAcctInfoJson())) {
            List<PayOrderFundDto> accounts = JsonUtils.parseArray(payOrderDO.getAcctInfoJson(), PayOrderFundDto.class);
            Set<String> accountNoSet = accounts.stream().map(account -> account.getAccountNo()).collect(Collectors.toSet());
            HeliPayAppChannelParams heliPayAppChannelParams = heliPaySceneCommonPayOrder.heliPayAppChannelParams(channelPayOrderContext.getMerchantAppChannelDO());
            if (accountNoSet.contains(heliPayAppChannelParams.getMerchantNo())) {
                channelPayOrderContext.setRemark("合利宝分账规则,分账账号不可以传分账发起方自己");
                channelPayOrderContext.setProcessStatus(Boolean.FALSE);
                return;
            }
            channelPayOrderContext.setAccounts(accounts);
        }
        extensionExecutor.executeVoid(HeliPayScenePayOrderExtPt.class, BizScenario.valueOf(huiFuPay), ex -> ex.submitChannelPayOrder(channelPayOrderContext));
    }

    @Override
    public void queryChannelPayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        PayWayDO payWayDO = channelPayOrderQueryContext.getPayWayDO();
        PayOrderDO payOrderDO = channelPayOrderQueryContext.getPayOrderDO();
        String huiFuPay = heliPayType(payWayDO, payOrderDO.getExtra());
        if (!StringUtils.hasText(huiFuPay)) {
            channelPayOrderQueryContext.setRemark("当前支付方式不支持");
            channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        extensionExecutor.executeVoid(HeliPayScenePayOrderExtPt.class, BizScenario.valueOf(huiFuPay), ex -> ex.queryChannelPayOrder(channelPayOrderQueryContext));
    }

    @Override
    public void closeChannelPayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        PayWayDO payWayDO = channelPayOrderQueryContext.getPayWayDO();
        PayOrderDO payOrderDO = channelPayOrderQueryContext.getPayOrderDO();
        String huiFuPay = heliPayType(payWayDO, payOrderDO.getExtra());
        if (!StringUtils.hasText(huiFuPay)) {
            channelPayOrderQueryContext.setRemark("当前支付方式不支持");
            channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        extensionExecutor.executeVoid(HeliPayScenePayOrderExtPt.class, BizScenario.valueOf(huiFuPay), ex -> ex.closeChannelPayOrder(channelPayOrderQueryContext));
    }

    @Override
    public void queryChannelClosePayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        PayWayDO payWayDO = channelPayOrderQueryContext.getPayWayDO();
        PayOrderDO payOrderDO = channelPayOrderQueryContext.getPayOrderDO();
        String huiFuPay = heliPayType(payWayDO, payOrderDO.getExtra());
        if (!StringUtils.hasText(huiFuPay)) {
            channelPayOrderQueryContext.setRemark("当前支付方式不支持");
            channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        extensionExecutor.executeVoid(HeliPayScenePayOrderExtPt.class, BizScenario.valueOf(huiFuPay), ex -> ex.queryChannelClosePayOrder(channelPayOrderQueryContext));

    }

    @Override
    public boolean refundApply(ChannelRefundPayOrderContext channelRefundPayOrderContext) {
        return true;
    }

    @Override
    public void refundChannelPayOrder(ChannelRefundPayOrderContext channelRefundPayOrderContext) {
        PayWayDO payWayDO = channelRefundPayOrderContext.getPayWayDO();
        PayOrderDO payOrderDO = channelRefundPayOrderContext.getPayOrderDO();
        String huiFuPay = heliPayType(payWayDO, payOrderDO.getExtra());
        if (!StringUtils.hasText(huiFuPay)) {
            channelRefundPayOrderContext.setRemark("当前支付方式不支持");
            channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        extensionExecutor.executeVoid(HeliPayScenePayOrderExtPt.class, BizScenario.valueOf(huiFuPay), ex -> ex.refundChannelPayOrder(channelRefundPayOrderContext));
    }

    @Override
    public void queryRefundChannelPayOrder(ChannelRefundPayOrderContext channelRefundPayOrderContext) {
        PayWayDO payWayDO = channelRefundPayOrderContext.getPayWayDO();
        PayOrderDO payOrderDO = channelRefundPayOrderContext.getPayOrderDO();
        String huiFuPay = heliPayType(payWayDO, payOrderDO.getExtra());
        if (!StringUtils.hasText(huiFuPay)) {
            channelRefundPayOrderContext.setRemark("当前支付方式不支持");
            channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        extensionExecutor.executeVoid(HeliPayScenePayOrderExtPt.class, BizScenario.valueOf(huiFuPay), ex -> ex.queryRefundChannelPayOrder(channelRefundPayOrderContext));
    }

    @Override
    public CommonResult<PayOrderChannelResultDto> orderStatusParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        Map<String, String> params = HttpRequestUtil.requestFormParams(request);
        String response = JsonUtils.toJsonString(params);
        log.info("合利宝收到支付订单回调,参数数据为 payOrderDO:{}", response);
        CommonResult<PayOrderChannelResultDto> payOrderDOCommonResult = CommonResult.success();
        try {
            CommonResult<HeliPayConfig> commonResult = heliPayMerchantCommonParamsHandle.heliPayConfig(providerChannelConfigDO, true);
            // 配置转换
            HeliPayConfig heliPayConfig = commonResult.getData();
            if (CollectionUtils.isEmpty(payOrderSignFields)) {
                payOrderSignFields = new LinkedHashSet<>(Arrays.asList(HeliPayConstant.PAY_ORDER_NOTIFY_FIELD.split(",")));
            }
            boolean verify = HeliPaySM2Utils.verify(response, heliPayConfig, payOrderSignFields);
            if (!verify) {
                log.info("合利宝支付订单回调验签不通过,signature:{}", response);
                payOrderDOCommonResult.setCode(MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            payOrderDOCommonResult.setCode(MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode());
        }
        HeliPayOrderNotifyRes heliPayOrderQueryRes = JsonUtils.parseObject(response, HeliPayOrderNotifyRes.class);
        PayOrderDO updartePayOrderDO = new PayOrderDO();
        PayOrderChannelResultDto payOrderChannelResultDto = new PayOrderChannelResultDto();
        payOrderChannelResultDto.setUpdatePayOrderDO(updartePayOrderDO);
        payOrderChannelResultDto.setCallBackBody(response);
        switch (heliPayOrderQueryRes.getRt4_status()) {
            case "INIT":
            case "DOING":
                updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.PAYING.getCode());
                updartePayOrderDO.setBankMsg("");
                break;
            case "SUCCESS":
                updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.SUCCESS.getCode());
                updartePayOrderDO.setBankMsg("");
                break;
            case "FAIL":
                updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.FAIL.getCode());
                break;
            case "CLOSE":
                updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.CLOSE.getCode());
                break;
            case "CANCEL":
                updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.CANCEL.getCode());
                break;

        }
        if (StringUtils.hasText(heliPayOrderQueryRes.getRt12_orderCompleteDate())) {
            try {
                updartePayOrderDO.setSuccessTime(DateUtils.shanghaiDate(heliPayOrderQueryRes.getRt12_orderCompleteDate(), "yyyy-MM-dd HH:mm:ss"));
            }
            catch (Exception e) {
                log.error("合利宝支付订单返回 时间格式化错误 dateTime:{},message:{}", heliPayOrderQueryRes.getRt12_orderCompleteDate(), e.getMessage(),e);
            }
        }
        updartePayOrderDO.setPayOrderNo(heliPayOrderQueryRes.getRt2_orderId());
        updartePayOrderDO.setInsOrderNo(heliPayOrderQueryRes.getRt17_outTransactionOrderId());
        updartePayOrderDO.setChannelOrderId(heliPayOrderQueryRes.getRt3_systemSerial());
        updartePayOrderDO.setChannelOrderNo(heliPayOrderQueryRes.getRt11_channelOrderNum());
        if (StringUtils.hasText(heliPayOrderQueryRes.getChannelSettlementAmount())) {
            updartePayOrderDO.setSettlementAmount(MerchantUtils.stringToBigDecimal(heliPayOrderQueryRes.getChannelSettlementAmount()));
        }
        if (StringUtils.hasText(heliPayOrderQueryRes.getRt23_paymentAmount())) {
            updartePayOrderDO.setPayAmount(MerchantUtils.stringToBigDecimal(heliPayOrderQueryRes.getRt23_paymentAmount()));
        }
        if (StringUtils.hasText(heliPayOrderQueryRes.getRt10_openId())) {
            updartePayOrderDO.setUserId(heliPayOrderQueryRes.getRt10_openId());
        }
        else {
            updartePayOrderDO.setUserId(heliPayOrderQueryRes.getRt19_subOpenId());
        }
        payOrderDOCommonResult.setData(payOrderChannelResultDto);

        if (StringUtils.hasText(heliPayOrderQueryRes.getRuleJson())) {
            List<HeliPayOrderAccountRes> heliPayOrderAccountRes = JsonUtils.parseArray(heliPayOrderQueryRes.getRuleJson(), HeliPayOrderAccountRes.class);
            List<OrderFundDetailDO> orderFunds = new ArrayList<>();
            heliPayOrderAccountRes.stream().forEach(heliPayOrderAccountRes1 -> {
                OrderFundDetailDO orderAccount = new OrderFundDetailDO();
                orderAccount.setAmount(MerchantUtils.stringToBigDecimal(heliPayOrderAccountRes1.getSplitBillAmount()));
                orderAccount.setAccountNo(heliPayOrderAccountRes1.getSplitBillMerchantNo());
                orderAccount.setOutOrderNo(heliPayOrderAccountRes1.getSplitBillOrderNum());
                orderAccount.setBatchNo(heliPayOrderAccountRes1.getSplitBillRequestNo());
                orderAccount.setFailReason(heliPayOrderAccountRes1.getSplitBillRetMsg());
                if (StringUtils.hasText(heliPayOrderAccountRes1.getSplitBillOrderStatus())) {
                    switch (heliPayOrderAccountRes1.getSplitBillOrderStatus()) {
                        case "SUCCESS":
                            orderAccount.setFundStatus(PayOrderEnums.PAY_ORDER_FUND_STATUS.SUCCESS.getCode());
                            break;
                        case "FAILED":
                            orderAccount.setFundStatus(PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL.getCode());
                            break;
                        default:
                            orderAccount.setFundStatus(PayOrderEnums.PAY_ORDER_FUND_STATUS.FUNDING.getCode());
                    }
                }
                orderFunds.add(orderAccount);
            });
            payOrderChannelResultDto.setOrderFunds(orderFunds);
        }

        log.info("合利宝收到支付订单回调,解析后的订单数据为 payOrderDO:{}", JsonUtils.toJsonString(updartePayOrderDO));
        return payOrderDOCommonResult;
    }

    @Override
    public CommonResult<PayOrderChannelResultDto> orderStatusParse(HttpServletRequest request) {
        return null;
    }

    @Override
    public CommonResult<PayOrderChannelResultDto> merAuthParse(HttpServletRequest request) {
        return null;
    }

    @Override
    public PayOrderDO closeOrderStatusParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        return null;
    }

    @Override
    public CommonResult<CsOrderNotifyDto> csOrderStatusParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        return null;
    }

    @Override
    public CommonResult<RefundOrderNotifyDto> refundStatusParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        Map<String, String> params = HttpRequestUtil.requestFormParams(request);
        String response = JsonUtils.toJsonString(params);
        log.info("合利宝收到退款订单回调,参数数据为 payOrderDO:{}", response);
        String paramsConfig = providerChannelConfigDO.getParamsConfig();
        CommonResult<RefundOrderNotifyDto> payOrderDOCommonResult = CommonResult.success();
        try {
            if (CollectionUtils.isEmpty(refundOrderSignFields)) {
                refundOrderSignFields = new LinkedHashSet<>(Arrays.asList(HeliPayConstant.REFUND_ORDER_NOTIFY_FIELD.split(",")));
            }
            // 配置转换
            CommonResult<HeliPayConfig> commonResult = heliPayMerchantCommonParamsHandle.heliPayConfig(providerChannelConfigDO, true);
            // 配置转换
            HeliPayConfig heliPayConfig = commonResult.getData();
            boolean verify = HeliPaySM2Utils.verify(response, heliPayConfig, refundOrderSignFields);
            if (!verify) {
                log.info("合利宝退款订单回调验签不通过,signature:{}", response);
                payOrderDOCommonResult.setCode(MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            payOrderDOCommonResult.setCode(MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode());
        }
        HeliPayRefundNotifyRes heliPayRefundOrderQueryRes = JsonUtils.parseObject(response, HeliPayRefundNotifyRes.class);
        RefundOrderNotifyDto refundOrderNotifyDto = new RefundOrderNotifyDto();
        refundOrderNotifyDto.setCallBackBody(response);
        RefundOrderDO updateRefundOrderDO = new RefundOrderDO();
        refundOrderNotifyDto.setRefundOrder(updateRefundOrderDO);
        updateRefundOrderDO.setRefundOrderNo(heliPayRefundOrderQueryRes.getRt3_refundOrderId());
        switch (heliPayRefundOrderQueryRes.getRt5_status()) {
            case "FAIL":
                updateRefundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.FAIL.getCode());
                updateRefundOrderDO.setBankMsg(heliPayRefundOrderQueryRes.getRetReasonDesc());
                break;
            case "PART_REFUND":
                break;
            case "SUCCESS":
                updateRefundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.REFUND.getCode());
                if (StringUtils.hasText(heliPayRefundOrderQueryRes.getRt9_refundOrderCompleteDate())) {
                    try {
                        updateRefundOrderDO.setSuccessTime(DateUtils.shanghaiDate(heliPayRefundOrderQueryRes.getRt9_refundOrderCompleteDate(), "yyyy-MM-dd HH:mm:ss"));
                    }
                    catch (Exception e) {
                        log.error("合利宝退款订单查询返回 时间格式化错误 dateTime:{},message:{}", heliPayRefundOrderQueryRes.getRt9_refundOrderCompleteDate(), e.getMessage(),e);
                    }
                }
                break;
            default:
                updateRefundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.REFUNDING.getCode());
        }
        if (StringUtils.hasText(heliPayRefundOrderQueryRes.getRefundCashAmount())) {
            updateRefundOrderDO.setActualRefundAmount(MerchantUtils.stringToBigDecimal(heliPayRefundOrderQueryRes.getRefundCashAmount()));
        }
        payOrderDOCommonResult.setData(refundOrderNotifyDto);
        log.info("合利宝收到退款订单回调,解析后的订单数据为 payOrderDO:{}", JsonUtils.toJsonString(updateRefundOrderDO));
        return payOrderDOCommonResult;
    }

    @Override
    public String channelPayCode(PayWayDO payWayDO) {
        return heliPayType(payWayDO, "");
    }

    @Override
    public Long queryChannelPayAppid(ChannelPayOrderContext channelPayOrderContext) {
        PayWayDO payWayDO = channelPayOrderContext.getPayWayDO();
        PayOrderDO payOrderDO = channelPayOrderContext.getPayOrderDO();
        PayOrderCommonParamReq.Extra extra = channelPayOrderContext.getExtra();
        if (extra == null && StringUtils.hasText(payOrderDO.getExtra())) {
            extra = JsonUtils.parseObject(payOrderDO.getExtra(), PayOrderCommonParamReq.Extra.class);
        }
        PayEnums.PAY_TYPE payType = PayEnums.PAY_TYPE.fromByCode(payWayDO.getType());
        PayEnums.SCENE_PAY_TYPE scenePayType = PayEnums.SCENE_PAY_TYPE.fromByType(payWayDO.getSubType());
        ProviderChannelConfigDO providerChannelConfigDO = channelPayOrderContext.getProviderChannelConfigDO();
        if (extra != null && CommonEnum.YES_NO_TYPE.NO.getCode().equals(extra.getOriginalType()) && payType.getCode().equals(PayEnums.PAY_TYPE.WECHAT.getCode())) {
            switch (scenePayType) {
                case JSAPI:
                    return providerChannelConfigDO.getWechatDefaultPubId();
                case H5:
                case APP:
                    return providerChannelConfigDO.getWechatDefaultMiniId();
            }
        }
        return null;
    }

    @Override
    public String callBackResponse(CommonResult commonResult) {
        if (commonResult.isSuccess()) {
            return "success";
        }
        else {
            return commonResult.getMsg();
        }
    }

    @Override
    public boolean wechatLink(ChannelPayOrderContext channelPayOrderContext) {
        PayOrderCommonParamReq.Extra extra = channelPayOrderContext.getExtra();
        if (extra != null && !CommonEnum.YES_NO_TYPE.NO.getCode().equals(extra.getOriginalType())) {
            return false;
        }
        PayWayDO payWayDO = channelPayOrderContext.getPayWayDO();
        PayEnums.PAY_TYPE payType = PayEnums.PAY_TYPE.fromByCode(payWayDO.getType());
        PayEnums.SCENE_PAY_TYPE scenePayType = PayEnums.SCENE_PAY_TYPE.fromByType(payWayDO.getSubType());
        // 只有微信 app h5才需要
        switch (payType) {
            case WECHAT:
                break;
            default:
                return false;
        }
        switch (scenePayType) {
            case APP:
            case H5:
                break;
            default:
                return false;
        }
        // app H5 非原生支付才需要
        // 合利宝page 特殊
        // channelPayOrderContext.setPage("pages/user_recharge/user_recharge");
        return true;
    }

    /**
     * 汇付支付方式路由
     *
     * @param payWayDO
     * @return
     */
    private String heliPayType(PayWayDO payWayDO, String extraJson) {
        PayOrderCommonParamReq.Extra extra = null;
        if (StringUtils.hasText(extraJson)) {
            extra = JsonUtils.parseObject(extraJson, PayOrderCommonParamReq.Extra.class);
        }
        return heliPayType(payWayDO, extra);
    }

    /**
     * 汇付支付方式路由
     * 
     * @param payWayDO
     * @return
     */
    private String heliPayType(PayWayDO payWayDO, PayOrderCommonParamReq.Extra extra) {
        PayEnums.SCENE_PAY_TYPE scenePayType = PayEnums.SCENE_PAY_TYPE.fromByCode(payWayDO.getCode());
        if (scenePayType == null) {
            return null;
        }
        PayEnums.PAY_TYPE payType = PayEnums.PAY_TYPE.fromByCode(payWayDO.getType());
        switch (scenePayType) {
            case H5:
            case WAP:
                if (extra != null && CommonEnum.YES_NO_TYPE.NO.getCode().equals(extra.getOriginalType()) && payType.getCode().equals(PayEnums.PAY_TYPE.WECHAT.getCode())) {
                    return HeliPayExtConstant.MINI_APP;
                }
                return HeliPayExtConstant.H5;
            case JSAPI:
                switch (payType) {
                    case WECHAT:
                    case ALIPAY:
                        return HeliPayExtConstant.JSAPI_PAY;
                    default:
                        return HeliPayExtConstant.NATIVE_PAY;
                }
            case MINI_APP:
                switch (payType) {
                    case WECHAT:
                    case ALIPAY:
                        return HeliPayExtConstant.MINI_APP;
                    default:
                        return HeliPayExtConstant.NATIVE_PAY;
                }
            case APP:
                if (extra != null && CommonEnum.YES_NO_TYPE.NO.getCode().equals(extra.getOriginalType()) && payType.getCode().equals(PayEnums.PAY_TYPE.WECHAT.getCode())) {
                    return HeliPayExtConstant.MINI_APP;
                }
                switch (payType) {
                    case WECHAT:
                    case ALIPAY:
                        return HeliPayExtConstant.APP;
                    default:
                        return HeliPayExtConstant.NATIVE_PAY;
                }
            case SCAN:
                return HeliPayExtConstant.SCAN_PAY;
            default:
                return HeliPayExtConstant.NATIVE_PAY;
        }
    }

}
